package com.ranyk.www.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ranyk.www.base.BasePageDTO;
import com.ranyk.www.entity.AdminUserLoginLog;
import com.ranyk.www.mapper.AdminUserLoginLogMapper;
import com.ranyk.www.service.IAdminUserLoginLogService;
import com.ranyk.www.support.page.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CLASS_NAME: UserLoginLogServiceImpl.java <br/>
 *
 * @author ranyk           <br/>
 * @version V1.0           <br/>
 * @decription: 管理员登陆表 业务逻辑层接口实现  <br/>
 * @date: 2022-04-03 <br/>
 */
@Service
public class AdminUserLoginLogServiceImpl extends ServiceImpl<AdminUserLoginLogMapper, AdminUserLoginLog> implements IAdminUserLoginLogService {

    private AdminUserLoginLogMapper adminUserLoginLogMapper;

    @Autowired
    public void setUserLoginLogMapper(AdminUserLoginLogMapper adminUserLoginLogMapper) {
        this.adminUserLoginLogMapper = adminUserLoginLogMapper;
    }

    @Override
    public BasePageDTO<AdminUserLoginLog> listByUserId(Long userId, PageInfo pageInfo, String search) {
        Page<AdminUserLoginLog> page = new Page<>(pageInfo.getCurrent(), pageInfo.getLimit());
        RowBounds rowBounds = new RowBounds(pageInfo.getCurrent(), pageInfo.getLimit());
        List<AdminUserLoginLog> userLoginLogs = adminUserLoginLogMapper.listByPage(userId, pageInfo, search, rowBounds);
        pageInfo.setTotal(Long.valueOf(page.getTotal()).intValue());
        return new BasePageDTO<>(pageInfo, userLoginLogs);
    }

    /**
     * 通过用户ID,查询对应用户的登录日志
     *
     * @param currentUserId 需要查询的用户ID
     * @param pageInfo      分页对象
     * @param search        查询值
     * @return 返回查询结果 Map&lt;String,Object&gt; 集合
     */
    @Override
    public Map<String, Object> getLogsByUserId(long currentUserId, PageInfo pageInfo, String search) {
        Map<String, Object> result = new HashMap<>(4);
        BasePageDTO<AdminUserLoginLog> loginLogDTO = listByUserId(currentUserId, pageInfo, search);
        result.put("rows", loginLogDTO.getList());
        result.put("total", loginLogDTO.getPageInfo().getTotal());
        return result;
    }
}
